package controller;

import pojo.AjaxResult;
import pojo.ScheduleRequest;
import service.UserScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class ScheduleController {

    @Autowired
    private UserScheduleService userScheduleService;

    // 添加根路径映射
    @GetMapping("/")
    public String index() {
        return "redirect:/schedule/select";
    }

    @GetMapping("/schedule/select")
    public String showSchedulePage() {
        System.out.println("=== 访问排课页面 ===");
        return "schedule/select"; // 修正路径
    }

    /**
     * 提交空闲时间
     */
    @PostMapping("/schedule/submit")
    @ResponseBody
    public AjaxResult submitSchedule(@RequestBody ScheduleRequest request) {
        try {
            System.out.println("=== 接收到提交请求 ===");
            // 使用固定用户ID（测试用）
            Integer userId = 1;

            boolean success = userScheduleService.saveUserSchedule(userId, request);
            if (success) {
                return AjaxResult.success("空闲时间提交成功");
            } else {
                return AjaxResult.error("提交失败");
            }
        } catch (Exception e) {
            System.out.println("提交异常: " + e.getMessage());
            e.printStackTrace();
            return AjaxResult.error("提交失败: " + e.getMessage());
        }
    }


    // 添加测试接口
    @GetMapping("/schedule/test")
    @ResponseBody
    public String test() {
        return "服务器正常运行！";
    }
}